home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / Rubber Bandit / RubberBand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.0 KB  |  178 lines  |  [TEXT/KAHL]

  1.  
  2. /* =============================
  3.    Rubber Bandit is a simple
  4.    srcXor rubber-banding example.
  5.  
  6.    Think C 4.0.4 source.  
  7.   
  8.    - DMH, MacDTS, 3/5/91
  9.    =============================  */
  10.  
  11. /* constants:             */
  12.  
  13. #define testDLOG    1234
  14. #define OK_button    1
  15.  
  16. /* function prototypes: */
  17.  
  18. extern void HandleDLOG(DialogPtr theDialog);
  19. extern pascal Boolean RubberProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  20. extern void RubberBandIt(Point anchorPt);
  21. extern Rect *CheckRect(Rect *theRect);
  22. extern void DrawStuff(Rect *rubberRect);
  23. extern void    FanStuff(Rect *theRect);
  24.  
  25.  
  26. main() 
  27. {
  28.     DialogPtr    theDialog = NULL;
  29.  
  30.     InitGraf(&thePort);
  31.     InitFonts();
  32.     FlushEvents(everyEvent, 0);
  33.     InitWindows();
  34.     InitMenus();
  35.     TEInit();
  36.     InitDialogs(0L);
  37.     InitCursor();
  38.     
  39.     theDialog = GetNewDialog(testDLOG, NULL, (WindowPtr) -1L);
  40.     if (theDialog != NULL) HandleDLOG(theDialog);
  41.         
  42.     ExitToShell();
  43. }
  44.  
  45.  
  46. /*    ================================================
  47.     HandleDLOG calls ModalDialog and rubber-bands
  48.     until the OK button is pressed.
  49.     ================================================    */
  50.  
  51. void HandleDLOG(theDialog)
  52. DialogPtr theDialog;
  53. {
  54.     short        itemHit;
  55.     GrafPtr        oldPort;
  56.     Point        anchorPt;
  57.  
  58.     do
  59.     {
  60.         ModalDialog(NULL, &itemHit);
  61.  
  62.         if (itemHit != OK_button)
  63.         {
  64.             GetPort(&oldPort);            /* Save the port, set it to our dialog, */
  65.             SetPort(theDialog);
  66.             GetMouse(&anchorPt);        /* get the current mouse pos. and        */
  67.             RubberBandIt(anchorPt);        /* rubber-band the image.                */
  68.             SetPort(oldPort);            /* Restore the original port.            */
  69.         }
  70.     }
  71.     while (itemHit != OK_button);
  72.     
  73.     DisposDialog(theDialog);            /* Throw away the dialog.                */
  74. }
  75.  
  76.  
  77. /*    ================================================
  78.     RubberBandIt follows the mouse and rubber-bands
  79.     a design based on its position.  It retains
  80.     control until the mouse button is released.
  81.     ================================================    */
  82.  
  83. void RubberBandIt(anchorPt)
  84. Point    anchorPt;
  85. {
  86.     Point    curMousePt = {0, 0};
  87.     Rect    rubberRect;
  88.  
  89.     PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
  90.     PenPat(gray);
  91.  
  92.     GetMouse(&curMousePt);                            /* Get current mouse pos.        */
  93.  
  94.     rubberRect.top = anchorPt.v;                    /* Draw initial rectangle.        */
  95.     rubberRect.left = anchorPt.h;
  96.     rubberRect.bottom = curMousePt.v;
  97.     rubberRect.right = curMousePt.h;
  98.     DrawStuff(&rubberRect);
  99.  
  100.     while (Button())
  101.     {
  102.         GetMouse(&curMousePt);                        /* Get current mouse pos…        */
  103.  
  104.         if (curMousePt.h != rubberRect.right ||        /* If mouse moved…                */
  105.             curMousePt.v != rubberRect.bottom)
  106.         {
  107.             DrawStuff(&rubberRect);                    /* Erase old…                    */
  108.             rubberRect.bottom = curMousePt.v;
  109.             rubberRect.right = curMousePt.h;
  110.             DrawStuff(&rubberRect);                    /* and draw new.                */
  111.         }
  112.     }
  113.     
  114.     DrawStuff(&rubberRect);                            /* Erase old at end.            */
  115. }
  116.  
  117.  
  118. /*    ================================================
  119.     DrawStuff draws designs in the rectangle
  120.     passed, using the current pen mode, etc.
  121.     ================================================    */
  122.  
  123. void DrawStuff(rubberRect)
  124. Rect    *rubberRect;
  125. {
  126.     Rect    curRect;
  127.     
  128.     curRect = *rubberRect;
  129.     FrameRect(CheckRect(&curRect));
  130.     FanStuff(CheckRect(&curRect));
  131. }
  132.  
  133.  
  134. /*    ================================================
  135.     FanStuff draws a fanlike thing in the rect
  136.     passed, using the current pen mode, etc.
  137.     ================================================    */
  138.  
  139. void    FanStuff(theRect)
  140. Rect    *theRect;
  141. {
  142.     short    ang;
  143.  
  144.     for (ang = 0; ang < 360; ang += 60)
  145.         PaintArc(theRect, ang, 45);
  146. }
  147.  
  148.  
  149. /*    ================================================
  150.     CheckRect makes sure that the top of the
  151.     rectangle passed is ≤ the bottom and the left is
  152.     ≤ the right.  FrameRect needs things this way.
  153.     ================================================    */
  154.  
  155. Rect *CheckRect(theRect)
  156. Rect    *theRect;
  157. {
  158.     short    temp;
  159.  
  160.     if (theRect->top > theRect->bottom)        /* Need to reverse top and bottom? */
  161.     {
  162.         temp = theRect->top;
  163.         theRect->top = theRect->bottom;
  164.         theRect->bottom = temp;
  165.     }
  166.  
  167.     if (theRect->left > theRect->right)        /* Need to reverse left and right? */
  168.     {
  169.         temp = theRect->left;
  170.         theRect->left = theRect->right;
  171.         theRect->right = temp;
  172.     }
  173.     
  174.     return theRect;                            /* This makes nested calls easier.    */
  175. }
  176.  
  177.  
  178.